package {{table_info.name}}

import (
	"context"
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	api "{{package_info.api}}"
	dao "{{package_info.dao}}"
	model "{{package_info.model}}"
	do "{{package_info.do}}"
	service "{{package_info.service}}"
	SystemS "{{package_info.SystemS}}"
	liberr "{{package_info.liberr}}"
)

func init() {
	service.Register{{table_info.firstUpperName}}(New())
}

func New() *s{{table_info.firstUpperName}} {
	return &s{{table_info.firstUpperName}}{}
}

type s{{table_info.firstUpperName}} struct {
}

func (s s{{table_info.firstUpperName}}) List(ctx context.Context, req *api.{{table_info.firstUpperName}}ListReq) (total interface{}, {{table_info.camelName}}List []*model.{{table_info.firstUpperName}}Info, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.{{table_info.firstUpperName}}.Ctx(ctx)
		columns := dao.{{table_info.firstUpperName}}.Columns()
		//TODO 根据实际情况修改
		{% for item in field_info %}
        {% if item.camelName not in ['id','createdBy','updatedBy','createdAt','updatedAt'] %}
        if req.{{ item.firstUpperName }} != "" {
           m = m.Where(columns.{{ item.firstUpperName }}+" = ?", req.{{ item.firstUpperName }})
           // like
           //m = m.Where(fmt.Sprintf("%s like ?", columns.{{ item.firstUpperName }}), "%"+req.{{ item.firstUpperName }}+"%")
        }
        {% endif %}
        {% endfor %}

		total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取{{table_info.comment}}列表失败")
		orderBy := req.OrderBy
		if orderBy == "" {
			orderBy = "created_at desc"
		}
		err = m.Page(req.PageNum, req.PageSize).Order(orderBy).Scan(&{{table_info.camelName}}List)
		liberr.ErrIsNil(ctx, err, "获取{{table_info.comment}}列表失败")
	})
	return
}

func (s s{{table_info.firstUpperName}}) Add(ctx context.Context, req *api.{{table_info.firstUpperName}}AddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		// TODO 查询是否已经存在

		// add
		_, err = dao.{{table_info.firstUpperName}}.Ctx(ctx).Insert(do.{{table_info.firstUpperName}}{
		    {% for item in field_info %}
            {% if item.camelName not in ['id','createdBy','updatedBy','createdAt','updatedAt'] %}
            {{ item.firstUpperName }}:  req.{{ item.firstUpperName }},    // {{ item.comment }}
            {% endif %}
            {% endfor %}
            CreatedBy: SystemS.Context().GetUserId(ctx),
            UpdatedBy: SystemS.Context().GetUserId(ctx),
		})
		liberr.ErrIsNil(ctx, err, "新增{{table_info.comment}}失败")
	})
	return
}

func (s s{{table_info.firstUpperName}}) Edit(ctx context.Context, req *api.{{table_info.firstUpperName}}EditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = s.GetById(ctx, req.Id)
		liberr.ErrIsNil(ctx, err, "获取{{table_info.comment}}失败")
		//TODO 根据名称等查询是否存在

		//编辑
		_, err = dao.{{table_info.firstUpperName}}.Ctx(ctx).WherePri(req.Id).Update(do.{{table_info.firstUpperName}}{
		    {% for item in field_info %}
            {% if item.camelName not in ['createdBy','updatedBy','createdAt','updatedAt'] %}
            {{ item.firstUpperName }}:  req.{{ item.firstUpperName }},    // {{ item.comment }}
            {% endif %}
            {% endfor %}
		})
		liberr.ErrIsNil(ctx, err, "修改{{table_info.comment}}失败")
	})
	return
}

func (s s{{table_info.firstUpperName}}) Delete(ctx context.Context, id uint) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.{{table_info.firstUpperName}}.Ctx(ctx).WherePri(id).Delete()
		liberr.ErrIsNil(ctx, err, "删除{{table_info.comment}}失败")
	})
	return
}

func (s s{{table_info.firstUpperName}}) BatchDelete(ctx context.Context, ids []uint) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.{{table_info.firstUpperName}}.Ctx(ctx).Where(dao.{{table_info.firstUpperName}}.Columns().Id+" in(?)", ids).Delete()
		liberr.ErrIsNil(ctx, err, "批量删除{{table_info.comment}}失败")
	})
	return
}

func (s s{{table_info.firstUpperName}}) GetById(ctx context.Context, id uint) (res *model.{{table_info.firstUpperName}}Info, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.{{table_info.firstUpperName}}.Ctx(ctx).Where(fmt.Sprintf("%s=?", dao.{{table_info.firstUpperName}}.Columns().Id), id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取{{table_info.comment}}失败")
	})
	return
}
